<template>
  <div class="com-contatiner">
    <div class="com-chart" ref="screen_ref"></div>
    <span class="iconfont arr-left" :style="fontSize" @click="toLeft">&#xe6ef;</span>
    <span class="iconfont arr-right" :style="fontSize" @click="toRight">&#xe6ed;</span>
    <span class="cat-name" :style="fontSize">{{catNmae}}</span>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getThemeValue } from '@/utils/theme_utils'
export default {
  data () {
    return {
      chertInstane: null,
      allData: '',
      currentIndex: 0,
      titleFontSize: 0
    }
  },
  created () {
    this.$socket.registerCallBack('hotproductData', this.getData)
  },
  mounted () {
    this.initChart()
    // this.getData()
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
    this.$socket.send({
      action: 'getData',
      socketType: 'hotproductData',
      chartName: 'hotproduct',
      value: ''
    })
  },
  destroyed () {
    window.removeEventListener('resize', this.screenAdapter)
    this.$socket.unRegisterCallBack('hotproductData')
  },
  methods: {
    initChart () {
      this.chertInstane = this.$echarts.init(this.$refs.screen_ref, this.theme)
      // 初始化echarts的数据
      const initOption = {
        title: {
          text: '▍ 热销商品占比',
          top: 20,
          left: 20
        },
        legend: {
          icon: 'circle',
          top: '15%'
        },
        tooltip: {
          show: true,
          formatter: (reg) => {
            console.log(reg.data.children)
            const childrenData = reg.data.children
            // eslint-disable-next-line no-unused-vars
            let on = 0
            childrenData.forEach(item => {
              on += item.value
            })
            // eslint-disable-next-line no-unused-vars
            let data = ''
            childrenData.forEach(item => {
              data += `
              ${item.name} ${Math.round(item.value / on * 100)} %
              <br>
              `
            })
            return data
          }
        },
        series: [
          {
            type: 'pie'
          }
        ]
      }
      // 展示
      this.chertInstane.setOption(initOption)
    },
    // 获取服务器数据
    getData (res) {
      // const { data: res } = await this.$http.get('hotproduct')
      this.allData = res
      console.log(this.allData)
      // 调用updataChart方法
      this.updataChart()
    },
    // 处理数据
    updataChart () {
      const legendData = this.allData[this.currentIndex].children.map(val => {
        return val.name
      })
      const seriesData = this.allData[this.currentIndex].children.map(val => {
        return {
          name: val.name,
          value: val.value,
          children: val.children
        }
      })
      console.log(seriesData)
      const dataOption = {
        legend: {
          data: legendData
        },
        series: [
          {
            data: seriesData,
            label: {
              show: false,
              color: 'pink'
            },
            emphasis: {
              label: {
                show: true
              },
              labelLine: {
                show: false
              }
            }
          }
        ]
      }
      this.chertInstane.setOption(dataOption)
    },
    // 屏幕适配
    screenAdapter () {
      this.titleFontSize = (this.$refs.screen_ref.offsetWidth / 100) * 3.6
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: this.titleFontSize
          }
        },
        legend: {
          textStyle: {
            fontSize: this.titleFontSize / 1.2
          },
          itemWidth: this.titleFontSize / 1.2,
          itemHeight: this.titleFontSize / 1.2,
          itemGap: this.titleFontSize
        },
        series: [
          {
            radius: this.titleFontSize * 4.5,
            center: ['50%', '60%']
          }
        ]
      }
      this.chertInstane.setOption(adapterOption)
      this.chertInstane.resize()
    },
    toLeft () {
      this.currentIndex--
      if (this.currentIndex < 0) {
        this.currentIndex = this.allData.length - 1
      }
      this.updataChart()
    },
    toRight () {
      this.currentIndex++
      if (this.currentIndex > this.allData.length - 1) {
        this.currentIndex = 0
      }
      this.updataChart()
    }
  },
  computed: {
    catNmae () {
      if (!this.allData) {
        return ''
      } else {
        return this.allData[this.currentIndex].name
      }
    },
    fontSize () {
      return {
        fontSize: this.titleFontSize * 2 + 'px',
        color: getThemeValue(this.theme).titleColor
      }
    },
    // eslint-disable-next-line no-undef
    ...mapState(['theme'])
  },
  watch: {
    theme () {
      console.log('主题切换了')
      this.chertInstane.dispose() // 销毁当前的图表
      this.initChart() // 重新以最新的主题名称初始化图表对象
      this.screenAdapter() // 完成屏幕的适配
      this.updataChart() // 更新图表的展示
    }
  }
}
</script>

<style scoped>
.com-contatiner {
  position: relative;
}
.arr-left,
.arr-right {
  position: absolute;
  top:50%;
  transform: translateY(-50%);
  cursor: pointer;
  color: #fff;
}
.arr-left {
  left:10%;
}
.arr-right {
  right: 10%;
  transform: translateY(-50%);
}
.cat-name {
  position: absolute;
  left: 80%;
  bottom: 20px;
  color: #fff;
}
</style>
